//
//  PreferencesViewController.m
//  PentahoIPPUC
//
//  Created by William Seyler on 8/25/10.
//  Copyright 2010 William Seyler. All rights reserved.
//

#import "PreferencesViewController.h"
#import "PentahoIPPUCAppDelegate.h"

@implementation PreferencesViewController
@synthesize connectField;
@synthesize userIdField;
@synthesize passwordField;
@synthesize doneButton;
@synthesize cancelButton;

/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
}
*/

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (IBAction)textFieldDoneEditing:(id)sender {
	[sender resignFirstResponder];
}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}
- (void)viewDidLoad {
	PentahoIPPUCAppDelegate *appDelegate = (PentahoIPPUCAppDelegate *)[[UIApplication sharedApplication] delegate];
	[connectField setText:[appDelegate serverUrl]];
	[userIdField setText:[appDelegate userid]];
	[passwordField setText:[appDelegate password]];
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}

- (IBAction)apply:(id)sender {
	NSLog(@"in apply");
	PentahoIPPUCAppDelegate *appDelegate = (PentahoIPPUCAppDelegate *)[[UIApplication sharedApplication] delegate];
	NSString *oldUri = [appDelegate serverUrl];
	NSString *oldUser = [appDelegate userid];
	NSString *oldPassword = [appDelegate password];
	[oldUri retain];
	[oldUser retain];
	[oldPassword retain];
	
	[appDelegate setServerUrl:[connectField text]];
	[appDelegate setUserid:[userIdField text]];
	[appDelegate setPassword:[passwordField text]];
	
	BOOL result = [appDelegate connect];
	if (result) {
		[self cancel:sender];
	} else {
		UIActionSheet *errorSheet = [[UIActionSheet alloc] initWithTitle:@"Can't Connect" delegate:self cancelButtonTitle:@"Ok" destructiveButtonTitle:nil otherButtonTitles:nil];
		[errorSheet showInView:[self view]];
		[errorSheet release];
		
		[appDelegate setServerUrl:oldUri];
		[appDelegate setUserid:oldUser];
		[appDelegate setPassword:oldPassword];
		[oldUri release];
		[oldUser release];
		[oldPassword release];
	}
}

- (IBAction)cancel:(id)sender {
	[[self view] removeFromSuperview];
}

@end
